﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using System.IO;

namespace MasterPageGroupF3.AdminPages
{
    public partial class WebForm13 : System.Web.UI.Page
    {
        private int flag;
        private PRODUCT pro;
        protected void Page_Load(object sender, EventArgs e)
        {
            List<CITY> lcity = CITYBUS.LayBang().ToList();
            for (int i = 0; i < lcity.Count; i++)
            {
                ListItem l = new ListItem();
                l.Text = lcity[i].NAME;
                l.Value = lcity[i].IDCITY.ToString();
                DDCity.Items.Add(l);
            }
            List<PROVIDER> lprovider = PROVIDERBUS.Getlist();
            for (int i = 0; i < lprovider.Count; i++)
            {
                ListItem l = new ListItem();
                l.Text = lprovider[i].NAME;
                l.Value = lprovider[i].IDPROVIDER.ToString();
                DDProvider.Items.Add(l);
            }
            if (Session["IDPRODUCT"] != null && IsPostBack == false)
            {
                flag = 0;
                Page.Title = "Cập nhật sản phẩm";
                Name_Catalog.Text = "Cập nhật sản phẩm";
                

                PRODUCT product = PRODUCTBUS.GetProductById((int)Session["IDPRODUCT"]);
                ListItem ltem = new ListItem();
                ltem.Text = product.PROVIDER.NAME;
                ltem.Value = product.PROVIDER.IDPROVIDER.ToString();
                DDProvider.Items.Add(ltem);
                DDProvider.SelectedIndex = 0;

                ListItem ltem1 = new ListItem();
                ltem1.Text = product.CITY.NAME;
                ltem1.Value = product.CITY.IDCITY.ToString();
                DDCity.Items.Add(ltem1);
                DDCity.SelectedIndex = 0;

                tbPrice.Text = product.PRICE.ToString();
                tbRate.Text = product.RATE_DECREASE.ToString();
                tbMin.Text = product.MIN_NUMBER.ToString();
                tbMax.Text = product.MAX_NUMBER.ToString();

                String end = product.TIME_STOP.ToString();
                String[] mend = end.Split(' ');
                String[] mend1 = mend[0].Split('/');
                tbEnd.Text = String.Format("{0:s}-{1:s}-{2:s}", mend1[1], mend1[0], mend1[2]);

                String start = product.TIME_START.ToString();
                String[] mstart = start.Split(' ');
                String[] mstart1 = mstart[0].Split('/');
                tbBegin.Text = String.Format("{0:s}-{1:s}-{2:s}", mstart1[1], mstart1[0], mstart1[2]);

                tbStatus.Text = product.STATUS.ToString();
                tbSlogan.Text = product.SLOGAN;
                tbCondition.Text = product.CONDITION;
                tbHighlight.Text = product.HIGHLIGHT;
                tbContent.Text = product.CONTENT;
            }
            if (Session["IDPRODUCT"] == null)
            {
                flag = 1;
                Page.Title = "Thêm sản phẩm mới";
                Name_Catalog.Text = "Thêm sản phẩm mới";
            }
        }

        protected void Apply_Click(object sender, ImageClickEventArgs e)
        {
            int vali = 0;
            if (tbPrice.Text == "")
            {
                lberPrice.Text = "Không hợp lệ";
                vali = 1;
            }
            if (tbMax.Text == "")
            {
                lberMax.Text = "Không hợp lệ";
                vali = 1;
            }
            if (tbMin.Text == "")
            {
                lberMin.Text = "Không hợp lệ";
                vali = 1;
            }
            if (tbEnd.Text == "")
            {
                lberEnd.Text = "Không hợp lệ";
                vali = 1;
            }
            if (tbBegin.Text == "")
            {
                lberBegin.Text = "Không hợp lệ";
                vali = 1;
            }
            if (vali == 0)
            {
                
                    PRODUCT product = new PRODUCT();
                    product.CONTENT = tbContent.Text;
                    product.HIGHLIGHT = tbHighlight.Text;
                    product.CONDITION = tbCondition.Text;
                    product.SLOGAN = tbSlogan.Text;
                    product.IDPROVIDER = Int32.Parse(DDProvider.SelectedValue);
                    product.PRICE = Int32.Parse(tbPrice.Text);
                    product.IDCITY = Int32.Parse(DDProvider.SelectedValue);
                    product.MAX_NUMBER = Int32.Parse(tbMax.Text);
                    product.MIN_NUMBER = Int32.Parse(tbMin.Text);
                    product.RATE_DECREASE = Int32.Parse(tbRate.Text);
                    product.STATUS = Int16.Parse(tbStatus.Text);
                    
                    try
                    {
                        String[] day = tbBegin.Text.Split('-');
                        product.TIME_START = new DateTime(Int32.Parse(day[2]), Int32.Parse(day[1]), Int32.Parse(day[0]));
                        String[] day1 = tbEnd.Text.Split('-');
                        product.TIME_STOP = new DateTime(Int32.Parse(day1[2]), Int32.Parse(day1[1]), Int32.Parse(day1[0]));
                    }
                    catch (Exception ie)
                    {
                        lberBegin.Text = "Không hợp lệ";
                        return;
                    }
                    try
                    {
                        String[] day1 = tbEnd.Text.Split('-');
                        product.TIME_STOP = new DateTime(Int32.Parse(day1[2]), Int32.Parse(day1[1]), Int32.Parse(day1[0]));
                    }
                    catch (Exception ie)
                    {
                        lberEnd.Text = "Không hợp lệ";
                        return;
                    }


                    if (flag == 0)
                    {
                        product.IDPRODUCT = (int)Session["IDPRODUCT"];
                        PRODUCTBUS.UpdateProduct(product);
                        Session.Remove("IDPRODUCT");
                    }
                    else
                        PRODUCTBUS.Add_product(product);
                    if (fupload1.FileName != "" || fupload2.FileName != "" || fupload3.FileName != "")
                    {
                        String path = Server.MapPath("~\\ho-chi-minh\\images\\product\\") + product.IDPRODUCT.ToString();
                        Directory.CreateDirectory(path);
                        if (fupload1.FileName != "")
                            fupload1.PostedFile.SaveAs(path + "\\" + fupload1.FileName);
                        if (fupload2.FileName != "")
                            fupload2.PostedFile.SaveAs(path + "\\" + fupload2.FileName);
                        if (fupload3.FileName != "")
                            fupload3.PostedFile.SaveAs(path + "\\" + fupload3.FileName);

                        
                    }
                    
                    Response.Redirect("ManageProducts.aspx");
            }
        }

        protected void Cancel_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["IDPRODUCT"] != null)
                Session.Remove("IDPRODUCT");
            Response.Redirect("ManageProducts.aspx");
        }
    }
}